package com.syh.jn.scm.infrastructure.repository.impl.downstream.finance;

import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.downstream.entity.finance.DownCostBill;
import com.syh.jn.scm.domain.downstream.repository.finance.DownCostBillRepository;
import com.syh.jn.scm.dto.downstream.finance.DownCostBillVo;
import com.syh.jn.scm.infrastructure.repository.model.finance.IncomeBillModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

/**
 * Date:2018/7/19
 * Author:Clown
 * 下游费用单仓储实现;
 */
@Repository
public class DownCostBillRepositoryImpl extends AbstractAggregateRepository<DownCostBill, IncomeBillModel, Long> implements DownCostBillRepository {
  /**
   * 上传单据图片
   * @param bill
   */
  @Override
  public void upload(DownCostBill bill) {
    super.update(bill);
  }

  @Override
  protected Class getViewObject() {
    return DownCostBillVo.class;
  }

  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(SQLAliasGenerateUtil.aliasGenerate(IncomeBillModel.class, "downCostBill", null),
              "systemAccount.NAME AS brokerageName",
              "supplier.NAME AS supplierName");
      FROM("T_FINANCE_INCOME_BILL AS downCostBill");
      INNER_JOIN("T_BASE_SUPPLIER AS supplier ON supplier.ID = downCostBill.BUSINESS_ENTITY_ID","T_SYSTEM_ACCOUNT systemAccount on downCostBill.BROKERAGE_ID = systemAccount.ID");
    }};
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    Map<String, String> params = new HashMap<>();
    params.put("customerId", "downCostBill.CUSTOMER_ID");
    params.put("customerType","downCostBill.CUSTOMER_TYPE");
    params.put("state", "downCostBill.STATE");
    params.put("code", "downCostBill.CODE");
    params.put("confirmState", "downCostBill.CONFIRM_STATE");
    params.put("inputTimestamp", "downCostBill.INPUT_TIMESTAMP");
    params.put("statementOfAccountId", "downCostBill.STATEMENT_OF_ACCOUNT_ID");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return params.get(propertyName);
    });
    return parse;
  }
}
